/**
 * @Title: QzrServices.java
 * @Package com.haciai.services
 * @Description: TODO
 * Copyright: Copyright (c) 2013 
 * Company:淮安冲浪软件科技有限公司
 * 
 * @author zyue
 * @date 2013年8月15日 上午10:09:47
 * @version V1.0
 */

package com.clkj.sunhelp.mvp.seekhelpnew;

import com.clkj.sunhelp.mvp.seekhelpnew.domain.Qz_viewItem;
import com.clkj.sunhelp.mvp.seekhelpnew.domain.QzxxListItem;
import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: QzrServices
 * @Description: 获取救助人信息
 * @author zyue
 * @date 2013年8月15日 上午10:09:47
 * 
 */

public class QzrServices {
	/**
	 * select * from person order by personid asc limit 5 offset 3 select * from
	 * person order by personid asc limit 3,5 获取5条记录，跳过前面3条 上述两语句一样 分页获取记录
	 * 按从新到旧查询
	 * 
	 * @param offset
	 *            跳过前面多少条记录
	 * @param maxResult
	 *            每页获取多少条记录
	 * @return
	 */
	public static List<QzxxListItem> getScrollData(int offset, int maxResult,
												   String findParm) throws Exception {
		List<QzxxListItem> returnData = new ArrayList<QzxxListItem>();
		// http://192.168.2.84:8080/haciaiws/servlet/getMarkList
		String url = WebserviceConfig.SERVERADDRESS
				+ "servlet/getQzxxList?offset=" + offset + "&maxResult="
				+ maxResult + "&parm=" + findParm;
		HttpPost request = new HttpPost(url);
		// 发送请求
		HttpResponse httpResponse = new DefaultHttpClient().execute(request);
		// 得到应答的字符串，这也是一个 JSON 格式保存的数据
		String retSrc = EntityUtils.toString(httpResponse.getEntity());
		// 生成 JSON 对象
		JSONArray jsonArray = new JSONArray(retSrc);
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
			QzxxListItem entity = new QzxxListItem();
			// jsonObject2.getString("rOW_ID")
			entity.setRow_id(jsonObject2.getString("row_id"));
			entity.setTitle(jsonObject2.getString("title"));
			entity.setImgurl(jsonObject2.getString("imgurl"));
			returnData.add(entity);
		}
		// String token = result.get("token");
		return returnData;
	}
	
	public static Qz_viewItem getYgttDetailById(String rowId)throws Exception {
		String url = WebserviceConfig.SERVERADDRESS+"servlet/getQzxxDetail?row_id="+rowId;
		HttpPost request = new HttpPost(url);
		// 发送请求
		HttpResponse httpResponse = new DefaultHttpClient().execute(request);
		// 得到应答的字符串，这也是一个 JSON 格式保存的数据
		String retSrc = EntityUtils.toString(httpResponse.getEntity());
		Qz_viewItem obj = new Gson().fromJson(retSrc, Qz_viewItem.class);
		return obj;
	}

	public static List<QzxxListItem> getWyjzList(String extraText,
                                                 String extraDiqu, String extraAge, String extraQztime)throws Exception {
		List<QzxxListItem> returnData = new ArrayList<QzxxListItem>();
		//http://192.168.2.84:8080/haciaiws/servlet/getWyjzList?text=心脏病&diqu=淮安区&age=50-70岁&qztime=2013年
		String url = WebserviceConfig.SERVERADDRESS
				+ "servlet/getWyjzList?text="
				+extraText+"&diqu="
				+extraDiqu+"&age="+extraAge+"&qztime="+extraQztime;
		HttpPost request = new HttpPost(url);
		// 发送请求
		HttpResponse httpResponse = new DefaultHttpClient().execute(request);
		// 得到应答的字符串，这也是一个 JSON 格式保存的数据
		String retSrc = EntityUtils.toString(httpResponse.getEntity());
		// 生成 JSON 对象
		JSONArray jsonArray = new JSONArray(retSrc);
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
			QzxxListItem entity = new QzxxListItem();
			// jsonObject2.getString("rOW_ID")
			entity.setRow_id(jsonObject2.getString("row_id"));
			entity.setTitle(jsonObject2.getString("title"));
			entity.setImgurl(jsonObject2.getString("imgurl"));
			returnData.add(entity);
		}
		// String token = result.get("token");
		return returnData;
	}
	
}
